﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Coolite.Ext.Web;
using Sagacity.NC.Business.SysMange;

namespace Sagacity.NC.Web
{
    public partial class Login : System.Web.UI.Page
    {
        Sagacity.Build.User mUser = new Sagacity.Build.User();
        string postUserName = string.Empty;
        string postPWD = string.Empty;
        bool urlPass = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            postUserName = Request.QueryString["userName"] == null ? "" : Request.QueryString["userName"];
            postPWD = Request.QueryString["Password"] == null ? "" : Request.QueryString["Password"];

            if (!IsPostBack)
            {
                this.dateExcute.Value = DateTime.Now;
                this.sBar.Text = Config.BaseConfig.Instance.PageConfig.PageFooter;
            }

            if (!string.IsNullOrEmpty(postUserName))
            {
                btnLogin_Click(null, null);
                urlPass = true;
            }
        }

        protected void btnLogin_Click(object sender, AjaxEventArgs e)
        {
            string userName = txtUsername.Text.Trim();
            string password = txtPassword.Text.Trim();
            string bizDate = dateExcute.Value.ToString();

            if (string.IsNullOrEmpty(postUserName))
            {
                if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
                {
                    Ext.Msg.Alert("注意", "登陆失败，请检查用户名和密码。").Show();
                    winLogin.Show();
                    return;
                }

                if (bizDate == "0001-1-1 0:00:00")
                {
                    Ext.Msg.Alert("注意", "请选择业务处理日期。").Show();
                    winLogin.Show();
                    return;
                }
            }
            else
            {
                userName = postUserName;
                password = postPWD;
            }

            password = Common.AES.Encode(password, Config.BaseConfig.Instance.AES_EncryptKey);
            Sagacity.Entity.Build.UserInfo uInfo = mUser.UserLogin(userName, password);
            if (uInfo != null)
            {
                int selRoleId = int.Parse(hidSelectRole.Value);
                uInfo.LoginRole = uInfo.Roles.Find(o => o.Id == selRoleId);

                if (!string.IsNullOrEmpty(postUserName))
                {
                    uInfo.LoginRole = uInfo.Roles[0];
                }
                mUser.WriteCookie(uInfo, "Sagacity.HR_" + uInfo.Id.ToString());
                System.Web.Security.FormsAuthentication.SetAuthCookie(uInfo.Id.ToString(), false);

                new FormUser().SignIn(uInfo.UserName, DateTime.Parse(this.dateExcute.Value.ToString()));

                Response.Redirect("Index.aspx");
            }
            else
            {

                if (urlPass)
                    Ext.Redirect("Login.aspx");
                winLogin.Show();
                Ext.Msg.Alert("注意", "登陆失败，请检查用户名和密码。").Show();

            }
        }

        [AjaxMethod]
        public string GetUserRoles(string userName)
        {
            string roles = "[";
            Sagacity.Entity.Build.UserInfo uInfo = mUser.GetUserByName(userName);
            if (uInfo == null)
            {
                return "[]";
            }
            foreach (Sagacity.Entity.Build.RoleInfo r in uInfo.Roles)
            {
                roles += "{'Id':'" + r.Id + "','Caption':'" + r.Caption + "'},";
            }
            roles += "]";
            return roles.Length > 2 ? roles.Remove(roles.LastIndexOf(','), 1) : "[]";
        }
    }
}
